/**
 * AgaEngine (c)2011.
 * FileName:Rectangle.java
 */
package aga.engine.physic;

import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.World;
import org.jbox2d.dynamics.joints.RevoluteJoint;
import org.jbox2d.dynamics.joints.RevoluteJointDef;

import aga.engine.AgaCfg;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;

/**
 * Revolute Joint
 * @author User
 */
public class JointRevolute extends PhysicJoint {
    //===========================================================
    //Constants
    //===========================================================

    //===========================================================
    //Fields
    //===========================================================

    private Paint paint = new Paint();
    private Paint paintBorder = new Paint();

    //===========================================================
    //Constructors
    //===========================================================

    /**
     *
     * @param world
     * @param b1
     * @param b2
     */
    protected JointRevolute(World world, Body b1, Body b2) {
        super(world);

        paint.setColor(Color.RED);
        paintBorder.setColor(Color.GREEN);
        paintBorder.setStyle(Style.STROKE);
        paintBorder.setAntiAlias(true);

        RevoluteJointDef joinDef = new RevoluteJointDef();
        joinDef.initialize(b1, b2, b2.getWorldCenter());
        joinDef.enableMotor = true;
        joinDef.maxMotorTorque = 100;

        joint = world.createJoint(joinDef);
    }

    //===========================================================
    //Getter & Setter
    //===========================================================
    public RevoluteJoint getJoint() {
        return (RevoluteJoint) joint;
    }

    //===========================================================
    //Methods for/from SuperClass/Interfaces
    //===========================================================


    @Override
    public void draw(Canvas canvas) {
        final Vec2 pos = joint.getBodyB().getPosition();
        canvas.drawCircle(pos.x*AgaCfg.PHY2MAP_SCALE, pos.y*AgaCfg.PHY2MAP_SCALE, 1, paint);
    }

    //===========================================================
    //Methods
    //===========================================================

    //===========================================================
    //Inner and Anonymous Classes
    //===========================================================

    @Override
    public void destroy() {
        mWorld.destroyJoint(joint);
    }
}
